﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Common;

namespace Ewts.Trace
{
    public partial class BugAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtbugno.Text.Trim().Length == 0)
            {
                strErr += "BUG编号不能为空！\\n";
            }
            if (this.txttitle.Text.Trim().Length == 0)
            {
                strErr += "标题不能为空！\\n";
            }
            if (this.txtbuginfo.Text.Trim().Length == 0)
            {
                strErr += "BUG描述不能为空！\\n";
            }
            if (this.txturl.Text.Trim().Length == 0)
            {
                strErr += "链接不能为空！\\n";
            }
            if (!PageValidate.IsNumber(dropLevel.SelectedValue))
            {
                strErr += "请选择优先级别！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string bugno = this.txtbugno.Text;
            string title = this.txttitle.Text;
            string buginfo = this.txtbuginfo.Text;
            string url = this.txturl.Text;
            string bugimg = "/upload/bug/";

            if (bugImgUpload.PostedFile != null) 
            {
                string photoName1 = bugImgUpload.PostedFile.FileName; //获取初始文件名    
                int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引    
                string newext = photoName1.Substring(i); //获取文件扩展名    
                if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
                {
                    Response.Write("文件格式不正确!");
                    Response.End();
                }
                DateTime now = DateTime.Now; //获取系统时间    
                string photoName2 = now.Millisecond.ToString() + "_" + bugImgUpload.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名    
                bugimg = bugimg + photoName2;
                bugImgUpload.PostedFile.SaveAs(Server.MapPath(bugimg)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用""代替    

            }
            int buglevel = int.Parse(dropLevel.SelectedValue);

            DataLib.Model.BugTraceTab model = new DataLib.Model.BugTraceTab();
            model.bugno = bugno;
            model.title = title;
            model.buginfo = buginfo;
            model.url = url;
            model.bugimg = bugimg;
            model.status = 1;
            model.buglevel = buglevel;

            DataLib.BLL.BugTraceTab bll = new DataLib.BLL.BugTraceTab();
            bll.Add(model);
            DataLib.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "BugAdd.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("BugTraceList.aspx");
        }
    }
}